package com.framerock.server;

import com.framerock.baselib.Base;
import com.framerock.baselib.Patter;
import com.framerock.baselib.Rock;
import com.framework.ACore.Controller;


import java.util.Map;

public class HttpController {


    /**
     * 首字母大写
     * */
    private static String firsttoUpper(String str)
    {
        String str1 = str.toLowerCase();
        str1 = str1.substring(0,1).toUpperCase()+str1.substring(1);
        return str1;
    }

    /**
     * 读取请求控制器得到路径
     * 路径格式：/目录/控制器/方法,或者/控制器/方法
     * */
    private static Map<String, String> getController(HttpClient httpclient)
    {
        Map<String, String> da = Rock.getMap(new String[]{"Dir","", "Controller", "", "Action", ""});
        String router = httpclient.getRouter();
        if(Rock.isEmpt(router)) return da;
        String Controller="",Action = "index", Dir="";
        String arr[] = router.split("/");
        int len      = arr.length;
        if(len==4){
            Dir         = arr[1];
            Controller  = arr[2];
            Action      = arr[3];
        }else{
            Controller = arr[1];
            if(len==3)Action = arr[2];
        }
        da.put("Dir", Dir);
        da.put("Controller", Controller);
        da.put("Action", Action);
        return da;
    }

    /**
     * 请求控制器
     * */
    public static String getControllerBody(HttpClient httpclient)
    {
        String accept = httpclient.getAccept();
        if(Rock.contain(accept, "image"))return accept;
        if(httpclient.getMethod().equals("OPTIONS"))return "OPTIONS";

        Map<String, String> kzq = getController(httpclient);
        String controller   =kzq.get("Controller"),
                action      = kzq.get("Action"),
                dir         = kzq.get("Dir");

        String bstr  = "404 not content";

        if(!Rock.isEmpt(dir))if(dir.length()>0 && !Patter.iszgen(dir))dir = "";
        if(!Rock.isEmpt(controller))if(!Patter.iszgen(controller))controller = "Index";
        if(!Rock.isEmpt(action))if(!Patter.iszgen(action))action = "error";

        if(Rock.isEmpt(controller))return Response.returnError("not router", "198");
        controller  = firsttoUpper(controller);

        int dbxu    = Base.dbContcreate(); //创建数据库链接
        Request req = new Request(httpclient);
        Base.setRequest(dbxu, req);
        Controller kzqObj = Controller.get(dbxu, dir, controller);

        //需要权限验证
        bstr        = kzqObj.checkLogin();
        if(!Rock.isEmpt(bstr)){
            if(req.isError())bstr = req.getError();
            Base.dbContClose(dbxu);
            return bstr;
        }

        //需要验证单位的
        bstr        = kzqObj.checkCompany();
        if(!Rock.isEmpt(bstr)){
            if(req.isError())bstr = req.getError();
            Base.dbContClose(dbxu);
            return bstr;
        }

        //执行正在的方法
        bstr        = kzqObj.Query(action);
        if(req.isError())bstr = req.getError();
        Base.dbContClose(dbxu); //关闭数据库连接

        return bstr;
    }
}